nums=[2,2,1,1,1,2,2]
# nums=[1,2,2,3,4]
def majorityElement(nums):
    dictionary=dict()
    for i in range(len(nums)):
        if nums[i] in dictionary:
            dictionary[nums[i]]+=1
        else:
            dictionary[nums[i]]=1
    maxValue=0
    maxNum=0
    for key in dictionary:
        if dictionary[key]>maxValue:
            maxValue=dictionary[key]
            maxNum=key
    print(maxNum)

def majorityElement1(nums):
    maxNum,count=0,0
    for i in range(len(nums)):
        if count==0:
            maxNum=nums[i]
            count=1
        else:
            if nums[i]==maxNum:
                count+=1
            else:
                count-=1
    return maxNum

print(majorityElement1(nums))